package com.edu.dao;

import com.edu.entity.Diningcar;
import com.edu.utils.JdbcHelper;

import java.sql.SQLException;
import java.util.List;

/**
 * @author ZCN
 * @version 1.0
 * @date 2021/11/23 10:55
 */
public class DiningCarDao {
    // 插入购物车
    public void insert(Diningcar diningcar) throws SQLException {
        String sql = "insert into diningcar(userid,foodid,foodname,feature,material,price,picture,hits,comment,typename) " +
                "values(?,?,?,?,?,?,?,?,?,?)";
        JdbcHelper.update(sql,diningcar.getUserid(),diningcar.getFoodid(),diningcar.getFoodname(),diningcar.getFeature(),diningcar.getMaterial()
        ,diningcar.getPrice(),diningcar.getPicture(),diningcar.getHits(),diningcar.getComment(),diningcar.getTypename());
    }

    // 根据用户id，查询购物车列表
    public List<Diningcar> selectByUserid(Integer userid) throws SQLException{
        String sql = "select * from diningcar where flag = 1 and userid = ?";
        List<Diningcar> diningcars = JdbcHelper.query(sql, Diningcar.class, userid);
        return diningcars;
    }


    public Diningcar selectById(Integer id) throws SQLException {
        String sql = "select * from diningcar where id = ?";
        List<Diningcar> list = JdbcHelper.query(sql, Diningcar.class, id);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    public void updateFlagtoconsum(String ids) throws SQLException {
        String[] idarr = ids.split(",");
        for (String s : idarr) {
            String sql = "update diningcar set flag = 0 where id = ?";
            JdbcHelper.update(sql,Integer.valueOf(s));
        }

    }

}
